//
//  AddressViewController.m
//  Weiweiyunshi
//
//  Created by 张旭峰 on 2017/1/22.
//  Copyright © 2017年 王译. All rights reserved.
//

#import "AddressViewController.h"
#import "FriendlistTableViewController.h"
#import "OrgListTableViewController.h"
#import "PYSearch.h"
#import "PeopleModel.h"
#import "OrginformationController.h"
@interface AddressViewController ()<UIScrollViewDelegate,UISearchControllerDelegate,PYSearchViewControllerDelegate>
{
    PYSearchViewController *pySearchVc;
    NSMutableArray *searchDataSource;

}
@property(nonatomic,strong)UIButton *btn1;
@property(nonatomic,strong)UIButton * btn2;
@property(nonatomic,weak)UIScrollView* scrollView;
@property(nonatomic,strong)NSArray * buttons;
//@property(nonatomic,weak)UISearchBar* searchBar;
@property(nonatomic,strong)UISearchController * searchController;
@end

@implementation AddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    searchDataSource = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithRed:239 green:239 blue:244];
    [self  setupUI];
  [self createPySearchVC];
}


-(void)setupUI{

    
    _searchController =[[UISearchController alloc]initWithSearchResultsController:nil];
    _searchController.delegate = self;
    _searchController.dimsBackgroundDuringPresentation = NO;
    //    _searchController.obscuresBackgroundDuringPresentation = NO;
    _searchController.hidesNavigationBarDuringPresentation = NO;
    self.definesPresentationContext = YES;
    [self.view addSubview:_searchController.searchBar];
    
    [_searchController.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(-10);
        make.right.offset(10);
        make.height.equalTo(@30);
        make.top.offset(80);
        
    }];

    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *rightButton = [[UIButton alloc] init];
    //    leftButton.backgroundColor=[UIColor lightGrayColor];
    [leftButton setBackgroundColor:[UIColor colorWithRed:255 green:255 blue:255]];
    
    [leftButton setTitle:@"我的联系人" forState:UIControlStateNormal];
    
    [rightButton setTitle:@"企业通讯录" forState:UIControlStateNormal];
    //    [rightButton setBackgroundColor:[UIColor colorWithRed:255 green:255 blue:255]];
    [rightButton setBackgroundColor:[UIColor lightGrayColor]];
    [leftButton setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor colorWithHex:666666] forState:UIControlStateNormal];
    self.btn1=leftButton;
    [self.view addSubview:leftButton];
    
    
    self.btn2=rightButton;
    [self.view addSubview:rightButton];
    
    NSArray *buttons=@[leftButton,rightButton];
    for(int i=0;i<buttons.count;i++) {
        
        
        UIButton *btn=buttons[i];
        btn.titleLabel.font=[UIFont systemFontOfSize:15];
        //[btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        //        [btn setBackgroundColor:[UIColor whiteColor]];
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        btn.tag=i;
        //        [topView addSubview:btn];
        
    }
    for (int i = 0; i < buttons.count - 1; i++) {
        UIButton* currentBtn = buttons[i];
        UIButton* nextBtn = buttons[i + 1];
        
        if (i == 0) { // 如果是第一个需要设置左边
            [currentBtn mas_makeConstraints:^(MASConstraintMaker* make) {
                make.left.offset(0);
            }];
        }
        
        if (i == buttons.count - 2) { // 如果是最后一个需要设置右边
            [nextBtn mas_makeConstraints:^(MASConstraintMaker* make) {
                make.right.offset(0);
            }];
        }
        
        [nextBtn mas_makeConstraints:^(MASConstraintMaker* make) {
            // 后一个和前一个等宽
            make.width.equalTo(currentBtn);
            // 后一个的左边 和 前一个的右边 一样
            make.left.equalTo(currentBtn.mas_right).offset(5);
            
        }];
    }
    [buttons mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(120);
        make.height.equalTo(@30);
    }];
    

    
    UIScrollView* scrollView = [[UIScrollView alloc] init];
    scrollView.pagingEnabled = YES; // 设置分页
    //    scrollView.backgroundColor = [UIColor orangeColor];
    
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self; // 设置代理对象 监听滑动
    [self.view addSubview:scrollView];
    FriendlistTableViewController* vc1 = [[FriendlistTableViewController alloc] init];
    OrgListTableViewController* vc2 = [[OrgListTableViewController alloc] init];
    UIButton *bottombutton = [[UIButton alloc]init];
    bottombutton.backgroundColor = [UIColor lightGrayColor];
    //    self.btn3 = bottombutton;
    //    [scrollView addSubview:bottombutton];
    
    
    // 把控制器的view添加到scrollView中
    [scrollView addSubview:vc1.view];
    [scrollView addSubview:vc2.view];
    
    // 建立父子关系
    [self addChildViewController:vc1];
    [self addChildViewController:vc2];
    
    //    [self.vcCache setObject:bottombutton forKey:@"b"];
    
    // 告诉系统父子关系已经建立
    [vc1 didMoveToParentViewController:self];
    [vc2 didMoveToParentViewController:self];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(_btn1.mas_bottom);
    }];
    
    [vc1.view mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.offset(0);
        make.left.bottom.offset(0);
        make.width.equalTo(scrollView);
//        make.height.equalTo(scrollView);
        make.bottom.offset(-44);
    }];
    //    [bottombutton mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.top.right.offset(0);
    //        make.left.equalTo(vc1.view.mas_right);
    //        make.width.equalTo(vc1.view);
    //        make.width.equalTo(scrollView);
    //        make.height.equalTo(@30);
    //
    //    }];
    
    [vc2.view mas_makeConstraints:^(MASConstraintMaker* make) {
        //       make.top.offset(30);
        //        make.top.equalTo(bottombutton.mas_bottom);
        make.top.bottom.offset(0);
        make.left.equalTo(vc1.view.mas_right);
        make.width.equalTo(vc1.view);
        make.width.equalTo(scrollView);
        make.height.equalTo(scrollView);
    }];
    
    self.scrollView = scrollView;
    
    self.buttons=buttons;
    
}
- (void)buttonClick:(UIButton*)sender
{
    
    [self.scrollView setContentOffset:CGPointMake(sender.tag * self.scrollView.bounds.size.width, 0) animated:NO];
    if(sender.tag==0)
    {
        [self.btn1 setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
        self.btn1.backgroundColor=[UIColor colorWithRed:255 green:255 blue:255];
        [self.btn2 setTitleColor:[UIColor colorWithHex:666666] forState:UIControlStateNormal];
        self.btn2 .backgroundColor=[UIColor lightGrayColor];
    }else{
        [self.btn1 setTitleColor:[UIColor colorWithHex:666666] forState:UIControlStateNormal];
        self.btn1.backgroundColor=[UIColor lightGrayColor];
        [self.btn2 setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
        self.btn2.backgroundColor=[UIColor colorWithRed:255 green:255 blue:255];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)createPySearchVC{
    pySearchVc = [PYSearchViewController searchViewControllerWithHotSearches:nil searchBarPlaceholder:@"搜索通讯录" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        //        if(searchText.length)
        
        NSLog(@"回调");
    }];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"header_back_icon"] style:UIBarButtonItemStyleDone target:self action:@selector(pop)];
    pySearchVc.navigationItem.leftBarButtonItem = backItem;

    pySearchVc.delegate = self;
}


-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
    
}



//点击搜索建议时
- (void)searchViewController:(PYSearchViewController *)searchViewController didSelectSearchSuggestionAtIndex:(NSInteger)index searchText:(NSString *)searchText{
    
    OrginformationController *orgVc = [[OrginformationController alloc]init];
    orgVc.userName = [searchDataSource[index]userName];
    orgVc.email = [searchDataSource[index]email];
    orgVc.mobile = [searchDataSource[index]mobile];
    orgVc.deptName = [searchDataSource[index]pdeptName];
    orgVc.pdeptName = [searchDataSource[index]deptName];
    orgVc.vidyoKey = [searchDataSource[index]vidyoKey];
    orgVc.img = [searchDataSource[index]img];
    [searchViewController.navigationController pushViewController:orgVc animated:YES];
}

-(void)presentSearchController:(UISearchController *)searchController
{
    NSLog(@"------->werwerwer");
    [self.navigationController pushViewController:pySearchVc animated:YES] ;
    //    [searchBar resignFirstResponder];
    
    //    return YES;
}

//点击搜索时
- (void)searchViewController:(PYSearchViewController *)searchViewController didSearchWithsearchBar:(UISearchBar *)searchBar searchText:(NSString *)searchText{
#warning 残留bug
    [searchBar resignFirstResponder];
    
}

//搜索文本框变化时
-(void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)searchBar searchText:(NSString *)searchText
{
    if(searchText.length)
    {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSMutableArray *searchSuggestionsM = [NSMutableArray arrayWithArray:[PeopleModel searchWithCondition:searchText]];
            searchDataSource = searchSuggestionsM;
            NSMutableArray *nameArr = [NSMutableArray array];
            NSString *searchSuggestion;
            for(int i = 0 ;i<searchSuggestionsM.count;i++)
            {
                searchSuggestion = [searchSuggestionsM[i]userName];
                [nameArr addObject:searchSuggestion];
            }
            searchViewController.searchSuggestions=nameArr;
        });
    }
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetX = scrollView.contentOffset.x;
    if(offsetX ==0)
    {
        [self.btn1 setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
        self.btn1.backgroundColor=[UIColor colorWithRed:255 green:255 blue:255];
        [self.btn2 setTitleColor:[UIColor colorWithHex:666666] forState:UIControlStateNormal];
        self.btn2 .backgroundColor=[UIColor lightGrayColor];
        
    }else if(offsetX == [UIScreen mainScreen].bounds.size.width)
    {
        
        [self.btn1 setTitleColor:[UIColor colorWithHex:666666] forState:UIControlStateNormal];
        self.btn1.backgroundColor=[UIColor lightGrayColor];
        [self.btn2 setTitleColor:[UIColor colorWithHex:0x0073dd] forState:UIControlStateNormal];
        self.btn2.backgroundColor=[UIColor colorWithRed:255 green:255 blue:255];
        
        
    }
    else
    {
        
    }
}


@end
